<template>
<!-- 	<view>
		<button @click="wechatPay">立即支付</button>
	</view> -->
	<view class="container bg-white">
		<uni-card :is-shadow="false" is-full>
			<text class="uni-h6">请勿通过其他途径付款，以免造成不必要的经济损失。投诉电话18085571310 18585856331 24小时设备故障服务电话0855-8123168</text>
		</uni-card>
		<uni-card title="合同名称" >
			<text class="uni-body">{{orderInfo.contractName}}</text>
		</uni-card>
		<uni-card title="客户名称" >
			<text class="uni-body">{{orderInfo.customerName}}</text>
		</uni-card>
		<uni-card title="联系人" >
			<text class="uni-body">{{orderInfo.contacts}}</text>
		</uni-card>
		<uni-card title="联系电话" >
			<text class="uni-body">{{orderInfo.telephone}}</text>
		</uni-card>
		<uni-card title="收款金额" >
			<text class="uni-body">¥{{orderInfo.contractAmount}}</text>
		</uni-card>
		<uni-card title="付款方式" >
			<text class="uni-body">微信</text>
		</uni-card>
		<uni-card title="收款人" >
			<text class="uni-body">凯里市中程信息技术服务部</text>
		</uni-card>
		<uni-card title="" >
			<text class="uni-body"></text>
		</uni-card>
		<view class="goods-carts">
			<uni-goods-nav :options="options" :fill="true" :button-group="customButtonGroup" @click="onClick"
				@buttonClick="wechatPay" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				openId: '',
				orderSn:'',
				options:[],
				orderInfo:{},
				customButtonGroup: [{
					text: '立即支付',
					backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
					color: '#fff'
				}]
			}
		},
		onLoad(options) {
			const openId = window.location.search.match(new RegExp('[?&]openId=([^&]+)'))[1];
			const orderId = window.location.search.match(new RegExp('[?&]orderId=([^&]+)'))[1];
			 console.log('查询参数1:', openId);
			 this.orderSn = orderId;
			    console.log('页面参数2:', this.orderSn);
		    this.openId = openId;
			this.getOrderInfo();
		  },
		methods: {
			// 微信JSAPI支付方法
			wechatPay() {
				this.getWechatPayParams();
			},
			buttonClick(e) {
				console.log(e)
			},
			getOrderInfo() {
				this.$http.post('/wxpay/getOrderInfo',{openId:this.openId,orderSn:this.orderSn}).then(res=>{
					//console.log("url", res)
					// console.log("res",res.data)
					//设置列表数据
					  if (res.data.success) {
						  this.orderInfo=res.data.result;
						  if(res.data.result.contractDescription=="SUCCESS"){
							  console.log("res",res.data.result)
							  this.$Router.push({name:'paysuccess', params:this.orderInfo});
						  }
					 }
					
				}).catch(()=>{
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			// 模拟从服务端获取支付参数
			getWechatPayParams() {
				this.$http.post('/wxpay/createOrder',{openId:this.openId,orderSn:this.orderSn}).then(res=>{
					//console.log("url", res)
					// console.log("res",res.data)
					//设置列表数据
					  if (res.data.success) {
						  this.totalPrice="000";
						  //alert(JSON.stringify(res.data.result));
						 console.log("res",res.data.result)
						 this.callWechatPay(res.data.result);
						 // this['dataCount'+idx]=res.data.result.total;
					 }
					
				}).catch(()=>{
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			// 调用微信JSAPI支付
			callWechatPay(payParams) {
				// alert("==="+JSON.stringify(payParams.nonceStr))
				// function onBridgeReady() {
				    WeixinJSBridge.invoke('getBrandWCPayRequest', {
						"appId": payParams.appId,     //公众号ID，由商户传入     
				        "timeStamp": ''+payParams.timeStamp,     //时间戳，自1970年以来的秒数     
				        "nonceStr": payParams.nonceStr,      //随机串     
				        "package": payParams.prepayId,     
				        "paySign": payParams.paySign //微信签名 
				    },
				    function(res) {
				        if (res.err_msg == "get_brand_wcpay_request:ok") {
				            // 使用以上方式判断前端返回,微信团队郑重提示：
				            //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠，商户需进一步调用后端查单确认支付结果。
				        }
				    });
				// }
				if (typeof WeixinJSBridge == "undefined") {
				    if (document.addEventListener) {
				        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
				    } else if (document.attachEvent) {
				        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
				        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
				    }
				} else {
				    onBridgeReady();
				}
			}
		}
	}
</script>

<style lang="scss">
.goods-carts {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		position: fixed;
		left: 0;
		right: 0;
		/* #ifdef H5 */
		left: var(--window-left);
		right: var(--window-right);
		/* #endif */
		bottom: 0;
	}
	.container {
		overflow: hidden;
	}

	.custom-cover {
		flex: 1;
		flex-direction: row;
		position: relative;
	}

	.cover-content {
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		height: 40px;
		background-color: rgba($color: #000000, $alpha: 0.4);
		display: flex;
		flex-direction: row;
		align-items: center;
		padding-left: 15px;
		font-size: 14px;
		color: #fff;
	}

	.card-actions {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
		height: 45px;
		border-top: 1px #eee solid;
	}
	.card-actions-item {
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.card-actions-item-text {
		font-size: 12px;
		color: #666;
		margin-left: 5px;
	}
	.cover-image {
		flex: 1;
		height: 150px;
	}
	.no-border {
		border-width: 0;
	}
</style>



